package com.craftcomputing.client.Entry;

import com.craftcomputing.client.Listeners.EntryPanelListeners;
import com.craftcomputing.client.Listeners.SearchResultLabelListeners.ImageLoadListener;
import com.craftcomputing.client.Listeners.SearchResultListeners.JSONLinkedEntryResponseTextHandler;
import com.craftcomputing.client.SearchResult.WorkshopSearchResult;
import com.craftcomputing.client.Entry.PhotoWidget.*;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.user.client.HTTPRequest;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.*;

public class CodeEntryPanel extends Composite {

	public CodeEntryPanel() {
		VerticalPanel panel = new VerticalPanel();
		initWidget(panel);
		panel.setStyleName("entryPanel");

		final VerticalPanel verticalPanel_4 = new VerticalPanel();
		panel.add(verticalPanel_4);
		panel.setCellVerticalAlignment(verticalPanel_4, HasVerticalAlignment.ALIGN_TOP);

		panel.add(linkedWorkshopsPanel);
		linkedWorkshopsPanel.setStyleName("gwt-paddedBottom");
		linkedWorkshopsPanel.setWidth("800px");
		panel.setCellVerticalAlignment(linkedWorkshopsPanel, HasVerticalAlignment.ALIGN_TOP);

		final HorizontalPanel horizontalPanel = new HorizontalPanel();
		linkedWorkshopsPanel.add(horizontalPanel);
		horizontalPanel.setSize("100%", "100%");
		horizontalPanel.setSpacing(4);

		horizontalPanel.add(workshopsUsedInLabel);
		workshopsUsedInLabel.setWidth("100%");
		horizontalPanel.setStyleName("gwt-SingleEntryTab");
		workshopsUsedInLabel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);

		linkedWorkshopsPanel.add(workshopsPanel);
		workshopsPanel.setSize("100%", "100%");
		workshopsPanel.setStyleName("gwt-TabPanel-SingleResult");

		workshopSearchResult.setStyleName("gwt-TabBar .gwt-Result");
		workshopsPanel.add(workshopSearchResult);
		workshopSearchResult.setSize("100%", "100%");

		verticalPanel_4.add(horizontalPanel_4);
		horizontalPanel_4.setWidth("100%");
		horizontalPanel_4.setHeight("100%");
		horizontalPanel_4.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);

		horizontalPanel_4.add(photo);
		horizontalPanel_4.setCellHorizontalAlignment(photo, HasHorizontalAlignment.ALIGN_LEFT);

		final VerticalPanel verticalPanel = new VerticalPanel();
		horizontalPanel_4.add(verticalPanel);
		horizontalPanel_4.setCellVerticalAlignment(verticalPanel, HasVerticalAlignment.ALIGN_MIDDLE);
		horizontalPanel_4.setCellHorizontalAlignment(verticalPanel, HasHorizontalAlignment.ALIGN_CENTER);
		verticalPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		//photo.setUrl("lightbox/close.gif");

		richTextArea.setWidth("455px");
		verticalPanel.add(richTextArea);
		richTextArea.setStyleName("gwt-RichTextArea");

		verticalPanel.add(logoblocksPanel);
		verticalPanel.add(picoblocksPanel);
		verticalPanel.add(textPanel);
		logoblocksPanel.setSpacing(4);
		picoblocksPanel.setSpacing(4);
		textPanel.setSpacing(4);

		final Image image = new Image();
		logoblocksPanel.add(image);
		image.setSize("20px", "20px");
		image.setUrl("mainimages/logoblocks.gif");

		final Image image2 = new Image();
		picoblocksPanel.add(image2);
		image2.setSize("20px", "20px");
		image2.setUrl("mainimages/picoblocks.gif");

		final Image image3 = new Image();
		textPanel.add(image3);
		image3.setSize("20px", "20px");
		image3.setUrl("mainimages/logo.gif");

		final Label cricketLogoblocksLabel = new Label("cricket logoblocks code:");
		logoblocksPanel.add(cricketLogoblocksLabel);
		logoblocksPanel.add(logoblocksViewHyperlink);
		logoblocksPanel.add(logoblocksDownloadHyperlink);

		final Label picoblocksLabel = new Label("picoblocks code:");
		picoblocksPanel.add(picoblocksLabel);
		picoblocksPanel.add(picoblocksViewHyperlink);
		picoblocksPanel.add(picoblocksDownloadHyperlink);

		final Label textLabel = new Label("logo code:");
		textPanel.add(textLabel);
		textPanel.add(textViewHyperlink);
		textPanel.add(textDownloadHyperlink);

	}
	public void update(JSONValue json){
    	final String workshopName = json.isObject().get("name").toString().replaceAll("\"", "");
    	String imageLoc = json.isObject().get("lbimage").toString().replaceAll("\"", "");
    	final String date = json.isObject().get("creationdate").toString().replaceAll("\"", "");
    	final String desc = json.isObject().get("desc").toString().replaceAll("\"", "");
    	final String author = json.isObject().get("author").toString().replaceAll("\"", "");
    	final String lbcodefile = json.isObject().get("lbcodefile").toString().replaceAll("\"", "");
    	final String lbimage = json.isObject().get("lbimage").toString().replaceAll("\"", "");
    	final String pbcodefile = json.isObject().get("pbcodefile").toString().replaceAll("\"", "");
    	final String pbimage = json.isObject().get("pbimage").toString().replaceAll("\"", "");
    	final String textfile = json.isObject().get("textfile").toString().replaceAll("\"", "");
		
		richTextArea.setHTML("Created by " + author + 
								" on " + date + "<br><br>" + 
								desc + "<br><br>"
								);
		if(json.isObject().get("workshops") != null){
			JSONArray workshops = json.isObject().get("workshops").isArray();
			if(workshops != null && workshops.size() > 0)
				workshopSearchResult.setLabels(workshops, "");
			else {
				linkedWorkshopsPanel.remove(workshopsPanel);
				workshopsUsedInLabel.setText("No Workshops use this Code");
			}
		}
		else{
			final int codeId = (int)Float.parseFloat(json.isObject().get("id1").toString());
			String searchText = "/python/workshops/fromcode?codeId=" + Integer.toString(codeId);
			HTTPRequest.asyncGet(searchText, new JSONLinkedEntryResponseTextHandler(workshopSearchResult));
		}		
		if(!lbcodefile.equals("")){
			logoblocksPanel.setVisible(true);

			logoblocksViewHyperlink.removeClickListener(logoblocksViewHyperlinkListener);
			logoblocksViewHyperlinkListener = new FileClickListener("/image/reg?path=/"+lbimage, "Logoblocks Code File");
			logoblocksViewHyperlink.addClickListener(logoblocksViewHyperlinkListener);

			logoblocksDownloadHyperlink.removeClickListener(logoblocksDownloadHyperlinkListener);
			logoblocksDownloadHyperlinkListener = new FileClickListener(lbcodefile, "Logoblocks Code File");
			logoblocksDownloadHyperlink.addClickListener(logoblocksDownloadHyperlinkListener);
		}
		else
			logoblocksPanel.setVisible(false);			

		if(!pbcodefile.equals("")){
			picoblocksPanel.setVisible(true);

			picoblocksViewHyperlink.removeClickListener(picoblocksViewHyperlinkListener);
			picoblocksViewHyperlinkListener = new FileClickListener("/image/reg?path=/"+pbimage, "Picoblocks Code File");
			picoblocksViewHyperlink.addClickListener(picoblocksViewHyperlinkListener);

			picoblocksDownloadHyperlink.removeClickListener(picoblocksDownloadHyperlinkListener);
			picoblocksDownloadHyperlinkListener = new FileClickListener(pbcodefile, "Picoblocks Code File");
			picoblocksDownloadHyperlink.addClickListener(picoblocksDownloadHyperlinkListener);

		}
		else
			picoblocksPanel.setVisible(false);			

		if(!textfile.equals("")){
			textPanel.setVisible(true);
			
			textViewHyperlink.removeClickListener(textViewHyperlinkListener);
			textViewHyperlinkListener = new FileClickListener(textfile, "Logo Code File");
			textViewHyperlink.addClickListener(textViewHyperlinkListener);

			textDownloadHyperlink.removeClickListener(textDownloadHyperlinkListener);
			textDownloadHyperlinkListener = new FileClickListener(textfile, "Logo Code File");
			textDownloadHyperlink.addClickListener(textDownloadHyperlinkListener);

		}
		else
			textPanel.setVisible(false);			

		if(lbimage.equals("") && pbimage.equals(""))
			imageLoc = "mainimages/01_11a.jpg";
		titleLabel.setText(workshopName);
		horizontalPanel_4.remove(photo);
		photo = new PhotoWidget(imageLoc);
		horizontalPanel_4.insert(photo, 0);

		
	}
	class FileClickListener implements ClickListener
	{
		String url, name;
		FileClickListener(String url, String name){
			this.url = url;
			this.name = name;
		}
		public void onClick(Widget w){
			Window.open(url, name, "");
		}
		
	}
	//private Image photo  = new Image("home.gif");	
	private VerticalPanel workshopsPanel = new VerticalPanel();
	private Label workshopsUsedInLabel = new Label("Workshops that use this Code");
	private VerticalPanel linkedWorkshopsPanel = new VerticalPanel();
	private FileClickListener picoblocksViewHyperlinkListener;
	private FileClickListener picoblocksDownloadHyperlinkListener;
	private FileClickListener logoblocksViewHyperlinkListener;
	private FileClickListener logoblocksDownloadHyperlinkListener;
	private FileClickListener textViewHyperlinkListener;
	private FileClickListener textDownloadHyperlinkListener;
	private HorizontalPanel logoblocksPanel = new HorizontalPanel();
	private HorizontalPanel picoblocksPanel = new HorizontalPanel();
	private HorizontalPanel textPanel = new HorizontalPanel();
	final Hyperlink logoblocksViewHyperlink = new Hyperlink("view", "");
	final Hyperlink logoblocksDownloadHyperlink = new Hyperlink("download", "");
	final Hyperlink picoblocksViewHyperlink = new Hyperlink("view", "");
	final Hyperlink picoblocksDownloadHyperlink = new Hyperlink("download", "");
	final Hyperlink textViewHyperlink = new Hyperlink("view", "");
	final Hyperlink textDownloadHyperlink = new Hyperlink("download", "");

	private PhotoWidget photo  = new PhotoWidget("lightbox/close.gif");	
	private Label titleLabel = new Label("Code Title");
	//private RichTextArea richTextArea = new RichTextArea();
	private HTML richTextArea = new HTML();
	private WorkshopSearchResult workshopSearchResult = new WorkshopSearchResult(false);
	private HorizontalPanel horizontalPanel_4 = new HorizontalPanel();
	public String getTitle(){
		return titleLabel.getText();
	}
}
